package com.qianfeng.xinyihezi.xinyihezi.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.qianfeng.xinyihezi.xinyihezi.customview.BanSlidingViewPager;
import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.adapter.ClassiftPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tcoy on 2016/11/12.
 */

public class ClassifyFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private String TAG = getClass().getSimpleName();
    private RadioGroup mRadioGroupModel;
    private RadioButton mRadioButtonClassift;
    private ImageView mImgSearch;
    private BanSlidingViewPager mViewPagerBanSliding;
    private List<Fragment> mFragmentList;
    private ClassiftPagerAdapter adapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classify, null);
        initView(view);
        initData(view);
        return view;
    }

    /**
     * 初始化数据
     */
    private void initData(View view) {
        mFragmentList = new ArrayList<>();
        ClassiftModelFragment classiftModelFragment = new ClassiftModelFragment();
        TagModelFragment tagModelFragment = new TagModelFragment();
        mFragmentList.add(classiftModelFragment);
        mFragmentList.add(tagModelFragment);
        adapter = new ClassiftPagerAdapter(getActivity().getSupportFragmentManager(), mFragmentList);
        mViewPagerBanSliding.setAdapter(adapter);
    }

    /**
     * 初始化View
     */
    private void initView(View view) {
        mRadioButtonClassift = (RadioButton) view.findViewById(R.id.radioButton_classify);
        mRadioGroupModel = (RadioGroup) view.findViewById(R.id.radioGroup_classify);
        mViewPagerBanSliding = (BanSlidingViewPager) view.findViewById(R.id.viewPagerBanSlidingClassift);
        mRadioGroupModel.setOnCheckedChangeListener(this);
        mImgSearch = (ImageView) view.findViewById(R.id.imageView_search);
        mImgSearch.setOnClickListener(this);
    }

    /**
     * 标题栏RadioGroup监听
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (mRadioButtonClassift.getId() == checkedId) {
            mViewPagerBanSliding.setCurrentItem(0, true);
        } else {
            mViewPagerBanSliding.setCurrentItem(1, true);
        }
    }

    /**
     * 所有的单击事件监听
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView_search:
                toastShow("搜索事件");
                break;
        }
    }

    /**
     * 测试用Toast
     */
    private void toastShow(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }
}
